﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FireTVcontrol
{
    public partial class FormMain : Form
    {
        bool _connected = false;
        bool _busy = false;
        string ipFilePath = String.Empty;

        public FormMain()
        {
            InitializeComponent();
            ClassAdb.startupFolder = Application.StartupPath + Path.DirectorySeparatorChar;
            ipFilePath = ClassAdb.startupFolder + "FireTVip.txt";
            if (File.Exists(ipFilePath))
            {
                try
                {
                    textBoxIP.Text = File.ReadAllText(ipFilePath).Trim();
                }
                catch
                {
                    textBoxIP.Text = "###.###.###.###";
                }
            }
        }

        private void buttonConnect_Click(object sender, EventArgs e)
        {
            if (_busy)
            {
                return;
            }
            _busy = true;
            string response = String.Empty;
            bool success = true;
            if (_connected)
            {
                success = ClassAdb.DisConnect(ref response);
                if (success)
                {
                    labelConnectStatus.Text = "Not connected";
                    _connected = false;
                    buttonConnect.Text = "Connect to FTV";
                    panel1.Visible = false;
                }
                else
                {
                    MessageBox.Show("Error on disconnect attempt:\n\n" + response);
                }
            }
            else  // not connected, trying to connect
            {
                labelConnectStatus.Text = "Connecting ...";
                Application.DoEvents();
                success = ClassAdb.Connect(textBoxIP.Text, ref response);
                if (success)
                {
                    labelConnectStatus.Text = "Connected";
                    buttonConnect.Text = "Disconnect";
                    _connected = true;
                    panel1.Visible = true;
                }
                else
                {
                    labelConnectStatus.Text = "Not connected";
                    MessageBox.Show("Failed to connect\n\n" + response);
                }
            }
            _busy = false;
        }

        private void ButtonAction(string code)
        {
            string response = String.Empty;
            bool rv = ClassAdb.AdbCommand("shell input keyevent " + code, ref response);
            if (!rv)
            {
                Application.DoEvents();
                MessageBox.Show(response);
            }
        }

        private void buttonSelect_Click(object sender, EventArgs e)
        {
            if (_busy)
            {
                return;
            }
            else
            {
                _busy = true;
                string code = (string)(((Button)sender).Tag);
                ButtonAction(code);
                _busy = false;
            }
        }

        private void textBoxIP_Leave(object sender, EventArgs e)
        {
            try
            {
                File.WriteAllText(ipFilePath, textBoxIP.Text.Trim());
            }
            catch
            {
            }
        }
    }
}
